// #include "Buffer.h"
// #include "Logger.h"

// using namespace devem;
using namespace std;

#include <sys/eventfd.h>
#include <functional>
#include <thread>

// #include "EventLoop.hpp"
// #include "TcpServer.h"

/**
 * epoll + 线程池
 * 将网络I/O与业务代码分离
 *  用户连接的创建和关闭  读写事件回调
 */

// void CreateEventFd() {
//     int event_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
//     if (event_fd < 0) {
//         LOG_WARN("create event_fd fail.");
//         abort();
//     }
//     cout << event_fd << endl;
// }

// class EchoServer {
//     // 1. 组合 TcpServer
//     // 2. 创建 EventLoop
//     //
// private:
//     void OnConnection(const PtrConnection& ptr) {
//         // cout something
//     }

//     void OnMessage(const PtrConnection& ptr, Buffer* buffer) {
//         // cout something
//     }

// public:
//     EchoServer(EventLoop* loop) : tcp_server_(loop), loop_(loop) {
//         tcp_server_.SetConnectedCallback(bind(&EchoServer::OnConnection, this, placeholders::_1));
//         tcp_server_.SetMessageCallback(bind(&EchoServer::OnMessage, this, placeholders::_1, placeholders::_2));
//         tcp_server_.SetThreadNum(3);
//     }

//     void start() {
//         tcp_server_.start();
//         // loop_->start();
//     }

// private:
//     TcpServer tcp_server_;
//     EventLoop* loop_;
// };

// class EchoServer {
// public:
//     void OnConnection(const PtrConnection& ptr) {
//         // cout something
//         cout << "OnConnection..." << endl;
//     }

//     void OnMessage(const PtrConnection& ptr, Buffer* buffer) {
//         // cout something
//         cout << "OnMessage..." << endl;
//     }

//     EchoServer() : server_() {
//         server_.SetConnectedCallback(bind(&EchoServer::OnConnection, this, placeholders::_1));
//         server_.SetMessageCallback(bind(&EchoServer::OnMessage, this, placeholders::_1, placeholders::_2));
//         server_.SetThreadNum(3);
//     }

//     void start() { server_.start(); }

// private:
//     TcpServer server_;
// };

#include "server.hpp"

class EchoServer {
private:
    TcpServer _server;

private:
    void OnConnected(const PtrConnection& conn) { DBG_LOG("NEW CONNECTION:%p", conn.get()); }
    void OnClosed(const PtrConnection& conn) { DBG_LOG("CLOSE CONNECTION:%p", conn.get()); }
    void OnMessage(const PtrConnection& conn, Buffer* buf) {
        conn->Send(buf->ReadPosition(), buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        conn->Shutdown();
    }

public:
    EchoServer(int port) : _server(port) {
        _server.SetThreadCount(2);
        _server.EnableInactiveRelease(10);
        _server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(
            std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Start() { _server.Start(); }
};

int main() {
    // Logger:LOG_DEBUG("have %d links", 28492);
    // Buffer:
    // Buffer buffer;
    // std::string str = "President Xi Jinpin";
    // std::string instr;
    // buffer.WriteString(str);
    // instr = buffer.PeekString(6);
    // buffer.printBuffer();
    // cout << "read buffer:" << instr << endl;
    // thread t1(CreateEventFd);
    // thread t2(CreateEventFd);

    EchoServer server(8080);
    server.Start();
    return 0;
}